// 专供购物车
var app = getApp();
var server = require('../../utils/server');
var common = require('../../utils/common');
Component({
  properties: {
    carts: {
      type: Object
    }
  },
  data: {
    startX: "",
    moveX: "",
    touch_end: "",
    touch_start: "",
    showDelPop: false,
    curEventId: '',
    curGoodsId: "",
    curGoodsIndex: "",
    curEventIndex: "",
    isAccount: false,
    accountParam: ""
  },
  methods: {
    //商品单选
    selectSingle(e) {
      var selectStatus = e.currentTarget.dataset.status,
        eventid = e.currentTarget.dataset.eventid,
        goods_id = e.currentTarget.dataset.goodsid;
      if (selectStatus == 0) {
        var param = {
          "event_id": eventid,
          "goods_id": goods_id,
          "is_selected": "1"
        }
        this.changeSelect(param)
      } else {
        var param = {
          "event_id": eventid,
          "goods_id": goods_id,
          "is_selected": "0"
        }
        this.changeSelect(param)
      }
    },
    selectAll(e) {
      var eventIndex = e.currentTarget.dataset.eventindex,
        eventid = e.currentTarget.dataset.eventid,
        selectStatus = e.currentTarget.dataset.selectstatus,
        goods = this.data.carts;
      var param = {
        "event_id": eventid,
        "goods_id": "",
        "is_selected": ""
      }
      if (selectStatus) {
        goods[eventIndex].isSelectAll = false;
        var newSelectStatus = "0";
        param.is_selected = newSelectStatus;
        this.changeSelect(param)
        this.setData({
          isAccount: true
        })
      } else {
        this.setData({
          isAccount: false
        })
        goods[eventIndex].isSelectAll = true;
        var newSelectStatus = "1";
        param.is_selected = newSelectStatus;
        this.changeSelect(param)
      }
    },
    //更改选择状态
    changeSelect(param) {
      server.postJSON(common.storePath + 'eventcart/select', param, res => {
        if (res.data.errno == '0') {
          if (res.data.errmsg != "") {
            common.showErrorToast(res.data.errmsg);
          }
          this._getCart();
        } else {
          common.showErrorToast(res.data.errmsg);
        }
      })
    },
    //判断按住商品是否为长按
    goToDetail(e) {
      var touchTime = this.data.touch_end - this.data.touch_start;
      if (touchTime >= 350) {
        this.deleteCurGoods(e)
      }
    },
    //左滑删除商品
    delByMove(e) {
      var eventid = e.currentTarget.dataset.eventid,
        goodsid = e.currentTarget.dataset.id;
      var param = {
        "event_id": eventid,
        "goods_id": [goodsid]
      }
      this.deleteGoods(param);
    },
    //删除商品
    deleteGoods(param) {
      server.postJSON(common.storePath + 'eventcart/delete', param, res => {
        if (res.data.errno == "0") {
          this._getCart();
          this.setData({
            showDelPop: false
          })
        } else {
          common.showErrorToast(res.data.errmsg);
        }
      })
    },
    //获取触摸屏幕开始时间及开始距离
    mytouchstart: function (e) {
      let that = this;
      that.setData({
        touch_start: e.timeStamp
      })
      if (e.touches.length == 1) {
        this.setData({
          startX: e.touches[0].clientX
        })
      }
    },
    //滑动事件结束  
    mytouchend: function (e) {
      var goodsIndex = e.currentTarget.dataset.index,
        eventIndex = e.currentTarget.dataset.eventindex;
      let that = this;
      that.setData({
        touch_end: e.timeStamp
      })
      var cart = this.data.carts;
      var endX = e.changedTouches[0].clientX;
      var disX = this.data.startX - endX;
      var txtStyle = disX > 50 / 2 ? "left:-50px;z-index:3;" : "left:0px;z-index:5;";
      cart[eventIndex].goods[goodsIndex].style = txtStyle;
      this.setData({
        carts: cart
      })
    },
    //取消触摸屏幕
    touchcancel(e) {
      var cart = this.data.carts,
        endX = e.changedTouches[0].clientX,
        disX = this.data.startX - endX,
        txtStyle = disX > 50 / 2 ? "left:-50px" : "left:0px",
        eventIndex = e.currentTarget.dataset.eventindex,
        goodsIndex = e.currentTarget.dataset.index;
      cart[eventIndex].goods[goodsIndex].style = txtStyle;
      this.setData({
        carts: cart
      })
    },
    //左滑获取移动距离
    touchmove(e) {
      if (e.touches.length == 1) {
        var moveX = e.touches[0].clientX;
        this.setData({
          moveX: moveX
        })
      }
    },
    deleteCurGoods(e) {
      var eventid = e.currentTarget.dataset.eventid,
        eventIndex = e.currentTarget.dataset.eventindex,
        goodsIndex = e.currentTarget.dataset.index,
        goodsid = e.currentTarget.dataset.id;
      this.data.curEventId = eventid;
      this.data.curGoodsId = goodsid;
      this.data.curEventIndex = eventIndex;
      this.data.curGoodsIndex = goodsIndex;
      this.data.deleteType = 0;
      this.setData({
        ['carts[' + eventIndex + '].goods[' + goodsIndex + '].isCanReduce']: false,
        showDelPop: true
      })
    },
    //整店一键删除
    deleteByEvent(e) {
      this.data.curEventId = e.currentTarget.dataset.eventid;
      this.data.curEventIndex = e.currentTarget.dataset.eventindex;
      this.data.deleteType = 1;
      this.setData({
        showDelPop: true
      })
    },
    confirmDel() {
      if (this.data.deleteType == 0) {
        this.setData({
          ['carts[' + this.data.curEventIndex + '].goods[' + this.data.curGoodsIndex + '].isCanReduce']: true
        })
        var param = {
          "event_id": this.data.curEventId,
          "goods_id": [this.data.curGoodsId]
        }
        this.deleteGoods(param);
      } else {
        let storeList = this.data.carts[this.data.curEventIndex].goods;
        var selectGoodsids = [];
        for (var i = 0; i < storeList.length; i++) {
          if (storeList[i].is_selected == 1) {
            selectGoodsids.push(
              storeList[i].goods_id
            )
          }
        }
        var param = {
          "event_id": this.data.curEventId,
          "goods_id": selectGoodsids
        }
        server.postJSON(common.storePath + 'eventcart/delete', param, res => {
          if (res.data.errno == "0") {
            this.setData({
              showDelPop: false
            })
            this._getCart();
          } else {
            common.showErrorToast(res.data.errmsg);
          }
        })
      }

    },
    cancelDel() {
      this.setData({
        showDelPop: false
      })
      if (this.data.deleteType == 0) {
        this.setData({
          ['carts[' + this.data.curEventIndex + '].goods[' + this.data.curGoodsIndex + '].isCanReduce']: true
        })
      }
    },
    //结算商品
    goToAccount(e) {
      app.globalData.zgAccountRandom = '';
      this.setData({
        isAccount: true,
        curEventIndex: e.currentTarget.dataset.eventindex
      })
      this.data.accountlist = [];
      let eventindex = e.currentTarget.dataset.eventindex,
        eventid = e.currentTarget.dataset.eventid;
      let cart = this.data.carts;
      let storeList = cart[eventindex].goods;
      for (var i = 0; i < storeList.length; i++) {
        if (storeList[i].is_selected == 1 && storeList[i].is_lack == 0) {
          this.data.accountlist.push({
            "goods_id": storeList[i].goods_id,
            "quantity": storeList[i].quantity
          })
        }
      }
      this.data.accountParam = {
        "address_id": app.globalData.addrId == null ? '' : app.globalData.addrId,
        "event_id": eventid,
        "coupon_list": [],
        "list": this.data.accountlist
      }
      if (this.data.accountlist.length > 0) {
        common.showLoading("结算中...");
        server.postJSON(common.storePath + 'event/check/', this.data.accountParam, res => {
          if (res.data.errno == '0') {
            res.data.data.event_id = eventid;
            wx.setStorage({
              key: 'memberaccountlist',
              data: res.data.data,
              success: res => {
                app.globalData.isEditToAccount = false;
                wx.setStorageSync('zgaccountparam', this.data.accountParam);
                app.globalData.isRefreshAccountList = false;
                wx.navigateTo({
                  url: '../../membership_supply/pages/accountMember',
                })
                this.setData({
                  isAccount: false,
                  curEventIndex: ''
                })
              }
            })
          } else {
            this.setData({
              isAccount: false,
              curEventIndex: ''
            })
            common.showErrorToast(res.data.errmsg)
          }
        })
      } else {
        common.showErrorToast("请先选择商品！");
        this.setData({
          isAccount: false
        })
      }
    },
    // 商品数量增加
    addCount(e) {
      var curtarget = e.currentTarget.dataset;
      var eventId = curtarget.eventid,
        goodsid = curtarget.id,
        goodsIndex = curtarget.index,
        quantity = curtarget.num,
        eventIndex = curtarget.eventindex;
      this.setData({
        ['carts[' + eventIndex + '].goods[' + goodsIndex + '].isCanAdd']: false
      })
      server.postJSON(common.storePath + 'eventcart/add', {
        "event_id": eventId,
        "goods_id": goodsid,
        "quantity": 1
      }, res => {
        if (res.data.errno == "0") {
          this._getCart();
        } else {
          this.setData({
            ['carts[' + eventIndex + '].goods[' + goodsIndex + '].isCanAdd']: true,
            ['carts[' + eventIndex + '].goods[' + goodsIndex + '].quantity']: parseInt(quantity),
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    },
    // 商品数量减少
    reduceCount(e) {
      var curtarget = e.currentTarget.dataset;
      var eventId = curtarget.eventid,
        goodsid = curtarget.id,
        goodsIndex = curtarget.index,
        quantity = curtarget.num,
        eventIndex = curtarget.eventindex;
      this.data.curEventId = eventId;
      this.data.curGoodsId = goodsid;
      this.data.curEventIndex = eventIndex;
      this.data.curGoodsIndex = goodsIndex;
      this.data.deleteType = 0;
      this.setData({
        ['carts[' + eventIndex + '].goods[' + goodsIndex + '].isCanReduce']: false,
      })
      if ((parseInt(quantity) - 1) > 0) {
        server.postJSON(common.storePath + 'eventcart/add', {
          "event_id": eventId,
          "goods_id": goodsid,
          "quantity": -1
        }, res => {
          if (res.data.errno == "0") {
            this._getCart();
          } else {
            this.setData({
              ['carts[' + eventIndex + '].goods[' + goodsIndex + '].isCanReduce']: true,
              ['carts[' + eventIndex + '].goods[' + goodsIndex + '].quantity']: parseInt(quantity),
            })
            common.showErrorToast(res.data.errmsg);
          }
        })
      } else {
        this.setData({
          showDelPop: true
        })
      }

    },
    //修改商品数量
    updateGoodsNum(e) {
      var curtarget = e.currentTarget.dataset;
      var eventId = curtarget.eventid,
        goodsid = curtarget.id,
        goodsIndex = curtarget.index,
        goodnum = e.detail.value,
        eventIndex = curtarget.eventindex,
        oldnum = curtarget.num;
      if (parseInt(goodnum) >= 1) {
        server.postJSON(common.storePath + 'eventcart/operate', {
          "event_id": eventId,
          "goods_id": goodsid,
          "quantity": goodnum
        }, res => {
          if (res.data.errno == "0") {
            this._getCart();
          } else {
            this.setData({
              ['carts[' + eventIndex + '].goods[' + goodsIndex + '].quantity']: parseInt(oldnum),
            })
            common.showErrorToast(res.data.errmsg);
          }
        })
      } else if (parseInt(goodnum) == 0) {
        server.postJSON(common.storePath + 'eventcart/operate', {
          "event_id": eventId,
          "goods_id": goodsid,
          "quantity": 1
        }, res => {
          if (res.data.errno == "0") {
            this._getCart();
          } else {
            this.setData({
              ['carts[' + eventIndex + '].goods[' + goodsIndex + '].quantity']: parseInt(oldnum),
            })
            common.showErrorToast(res.data.errmsg);
          }
        })
      }
    },
    //失去焦点获取商品数量
    getCurGoodsNum(e) {
      var curtarget = e.currentTarget.dataset;
      var eventId = curtarget.eventid,
        goodsid = curtarget.id,
        goodsIndex = curtarget.index,
        goodnum = e.detail.value,
        eventIndex = curtarget.eventindex,
        oldnum = curtarget.num;
      if (goodnum == '') {
        server.postJSON(common.storePath + 'eventcart/operate', {
          "event_id": eventId,
          "goods_id": goodsid,
          "quantity": 1
        }, res => {
          if (res.data.errno == "0") {
            this._getCart();
          } else {
            this.setData({
              ['carts[' + eventIndex + '].goods[' + goodsIndex + '].quantity']: parseInt(oldnum),
            })
            common.showErrorToast(res.data.errmsg);
          }
        })
      }
    },
    _getCart() {
      this.triggerEvent('getcart')
    }
  }
})